// index.js
// 获取应用实例
const app = getApp()

import user from '../../utils/user.js'
import {
    verifyPassword,
    changePassword
} from '../../api/app.js'

Page({
    data: {
        code: '',
        codes: [],
        type: 0,
        oldPassword: '',
        newPassword: '',
        isCodes:false
    },
    onLoad(option) {
        // 如果没有type也没有旧的密码，则设置密码
        if(!user.hasPassword && !option.type){
            this.setData({
                type: 'set'
            })
        }
        // type用于标识该次输入用途
        if (option.type) {
            this.setData({
                type: option.type
            })
        }
        // 重置密码时旧的密码用于校验是否本人
        if (option.oldPass) {
            this.setData({
                oldPassword: option.oldPass
            })
        }
        // 重置密码时的新密码
        if (option.newPass) {
            this.setData({
                newPassword: option.newPass
            })
        }
    },
    // 显示或隐藏密码
    onAppearCodes(){
        console.log('点击啦')
        this.setData({
            isCodes:!this.data.isCodes
        })
    },
    // 输入密码
    inputNumber(event) {
        console.log(event)
        // 将密码分为多个字符
        this.setData({
            ['codes']: event.detail.value.split('')
        })
        // 字符达到6个自动下一步
        if (event.detail.value.length === 6) {
            switch (this.data.type) {
                // 设置新密码
                case 'set':
                    wx.navigateTo({
                        url: '/pages/password/password?type=reSet&oldPass=' + this.data.oldPassword + '&newPass=' + event.detail.value
                    })
                    break
                // 再次输入新密码
                case 'reSet':
                    if(this.data.newPassword!==event.detail.value){
                        app.toast('两次输入密码不一致，请重新输入')
                        setTimeout(()=>{
                            wx.navigateTo({
                                url: '/pages/password/password?type=set&oldPass=' + this.data.oldPassword
                            })
                        },1000)
                        return 0
                    }
                    changePassword({
                        userId: user.userId,
                        password: event.detail.value,
                        oldPassword: this.data.oldPassword
                    }).then(()=>{
                        user.hasPassword = 1
                        wx.redirectTo({
                            url: '/pages/success/success?title=设置成功&content=密码已设置，请牢记您的密码&back=home'
                        })
                    })

                    break
                // 输入密码并返回到上一页校验
                case 'get':
                    {
                        const eventChannel = this.getOpenerEventChannel()
                        console.log(event.detail.value,'event.detail.value')
                        eventChannel.emit('submit', event.detail.value)
                        wx.navigateBack()
                    }
                    break
                // 设置新密码
                default:
                    // 校验旧密码
                    verifyPassword({
                        userId: user.userId,
                        password: event.detail.value
                    }).then(res => {
                        if (res) {
                            // 跳转后输入新密码
                            wx.navigateTo({
                                url: '/pages/password/password?type=set&oldPass=' + event.detail.value
                            })
                        } else {
                            this.setData({
                                code: '',
                                codes: []
                            })
                            app.toast('密码错误！')
                        }
                    })

            }
        }
    }
})
